package contegor.scire.apps;

import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;

import contegor.scire.core.Connectable;
import contegor.scire.structure.ContextListener;

public class ConnectableListModel extends AbstractListModel implements ContextListener {
  private static final long serialVersionUID = -2494889785345502830L;

  private List<Connectable> connectables = new ArrayList<Connectable>();

  @Override
  public Object getElementAt(int i) {
    return this.connectables.get(i);
  }

  @Override
  public int getSize() {
    return this.connectables.size();
  }

  @Override
  public void added(Connectable element) {
    int i = this.connectables.size();
    this.connectables.add(element);
    this.fireIntervalAdded(this, i, i+1);
  }

  @Override
  public void deleted(Connectable element) {
    int i = this.connectables.indexOf(element);
    this.connectables.remove(i);
    this.fireIntervalRemoved(this, i, i+1);
  }

  @Override
  public void updated(Connectable element) {
    // Element content has been already updated by the context, and this does not stores duplicates but references.
    int i = this.connectables.indexOf(element);
    this.fireContentsChanged(this, i, i+1);
  }
}
